package com.lznAdmin.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lznAdmin.system.entity.SysPermission;
import com.lznAdmin.system.mapper.SysPermissionMapper;
import com.lznAdmin.system.service.SysPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: lzn-manage-parent
 * @description:
 * @author: lzn
 * @create: 2021-09-02 17:59
 **/
@Slf4j
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService {

    @Override
    public List<SysPermission> findByUserId(Long userId) {
        if (userId == null) {
            return null;
        }
        List<SysPermission> sysPermissions = baseMapper.selectPermissionByUserId(userId);
        sysPermissions.remove(null);
        return sysPermissions;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean savePermission(SysPermission sysPermission) {

        switch (sysPermission.getType()) {
            case 0:
                log.info("Type:{} --- 类型:{}", sysPermission.getType(), "目录");
                sysPermission.setPath(null);
                sysPermission.setParentId(0L);
                sysPermission.setComponent("RouteView");
                sysPermission.setIsOutsideShow(0);
                boolean saveRootMenu = this.save(sysPermission);
                if (saveRootMenu) {
                    return true;
                }
                break;
            case 1:
                log.info("Type:{} --- 类型:{}", sysPermission.getType(), "菜单");
                sysPermission.setRedirect(null);
                sysPermission.setIcon(null);
                boolean saveMenus = this.save(sysPermission);
                if (saveMenus) {
                    return true;
                }
                break;
            case 2:
                log.info("Type:{} --- 类型:{}", sysPermission.getType(), "按钮/API");
                sysPermission.setRedirect(null);
                sysPermission.setIcon(null);
                sysPermission.setPath(null);
                sysPermission.setComponent(null);
                sysPermission.setIsOutsideShow(0);
                boolean saveAPI = this.save(sysPermission);
                if (saveAPI) {
                    return true;
                }
                break;
        }
        return false;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean updatePermission(SysPermission sysPermission) {
        switch (sysPermission.getType()) {
            case 0:
                log.info("Type:{} --- 类型:{}", sysPermission.getType(), "目录");
                sysPermission.setPath(null);
                sysPermission.setParentId(0L);
                sysPermission.setComponent("RouteView");
                sysPermission.setIsOutsideShow(0);
                boolean updateRootMenu = this.updateById(sysPermission);
                if (updateRootMenu) {
                    return true;
                }
                break;
            case 1:
                log.info("Type:{} --- 类型:{}", sysPermission.getType(), "菜单");
                sysPermission.setRedirect(null);
                sysPermission.setIcon(null);
                boolean updateMenus = this.updateById(sysPermission);
                if (updateMenus) {
                    return true;
                }
                break;
            case 2:
                log.info("Type:{} --- 类型:{}", sysPermission.getType(), "按钮/API");
                sysPermission.setRedirect(null);
                sysPermission.setIcon(null);
                sysPermission.setPath(null);
                sysPermission.setComponent(null);
                sysPermission.setIsOutsideShow(0);
                boolean updateBtnOrAPI = this.updateById(sysPermission);
                if (updateBtnOrAPI) {
                    return true;
                }
                break;
        }
        return false;
    }
}
